package com.lzq.mybatis.session;
import com.lzq.mybatis.conf.Configuration;
import com.lzq.mybatis.conf.MappedStatement;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Properties;

//实例化过程中加载配置文件信息到Configuration对象中
//生产sqlSession
public class sqlSessionFactory {
    private Configuration configuration = new Configuration();
    public sqlSessionFactory() {
        //加载db.properties配置信息到Configuration中。
        loadDBInfo();
        //加载mapper.xml文件信息到Configuration中。
        loadMapperInfo();
    }
    public sqlSession openSession(){
        return new DefaultSqlSession(configuration);
    }



    //记录mapper.xml文件的位置
    public static final String MAPPER_CONFIG_LOCATION = "mappers";
    //记录数据库配置文件存放位置（db.properties）
    public static final String DB_CONFIG_FILE = "db.properties";

    private void loadDBInfo(){
        //加载db.properties
        InputStream dbIn = sqlSessionFactory.class.getClassLoader().getResourceAsStream(DB_CONFIG_FILE);
        Properties properties = new Properties();
        try{
            properties.load(dbIn);
        }catch (Exception e){
            e.printStackTrace();
        }
        //将db.properties文件信息读入configuration对象中
        configuration.setJdbcDriver(properties.get("jdbc.driver").toString());
        configuration.setJdbcUrl(properties.get("jdbc.url").toString());
        configuration.setUserName(properties.get("jdbc.username").toString());
        configuration.setPassWord(properties.get("jdbc.password").toString());
    }

    private void loadMapperInfo(){
        URL resource = null;
        resource = sqlSessionFactory.class.getClassLoader().getResource(MAPPER_CONFIG_LOCATION);
        File mappers = new File(resource.getFile());
        //遍历mapper.xml加载到conf文件夹
        if(mappers.isDirectory()){
            File[] listFiles = mappers.listFiles();
            for(File file : listFiles){
                loadMapperInfo(file);
            }
        }
    }

    //加载单独的一个mapper.xml
    private void loadMapperInfo(File file) {
        //通过SAXReader读取文件
        SAXReader saxReader = new SAXReader();
        //通过read方法读取一个文件转化成Document文件
        Document document = null;
        try{
            document = saxReader.read(file);
        }catch (Exception e){
            e.printStackTrace();
        }
        //获取根节点元素，mapper
        Element root = document.getRootElement();
        //获取namespace元素
        String namespace = root.attribute("namespace").getData().toString();
        //获取select子列表元素
        List<Element> selects = root.elements("select");
        for(Element element : selects){
            //将获取到的内容存放到MappedStatement对象中
            MappedStatement mappedStatement = new MappedStatement();
            String resultType = element.attribute("resultType").getData().toString();
            String id = element.attribute("id").getData().toString();
            String sql = element.getData().toString();
            String sourceId = namespace + "." + id;
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sql);
            mappedStatement.setNamespace(namespace);
            mappedStatement.setSourceId(sourceId);
            configuration.getMappedStatements().put(sourceId,mappedStatement);
        }

    }


}
